import { Order } from '@/types/order'
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { dateAdd } from '@/utils/date'

const defaultImage = '/static/global/missing-face.png'
const initState = {
  masterUserId: undefined,
  masterUserName: '',
  masterUserAvatar: defaultImage,
  bookId: 0,
  bookName: '',
  borrowUserId: undefined,
  borrowUserName: '',
  borrowUserAvatar: '',
  deadline: dateAdd('M', 1),
  status: 0,
  createdTime: undefined,
  orderId: undefined,
  bookAvatar: '',
  bookAuthor: '',
  statusStr: '',
}

export const useOrderStore = defineStore(
  'order',
  () => {
    const order = ref<Order>({ ...initState })

    const setOrder = (val: Order) => {
      order.value = val
    }

    const clearOrder = () => {
      order.value = { ...initState }
    }

    return {
      order,
      setOrder,
      clearOrder,
      defaultImage,
    }
  },
  {
    persist: true,
  },
)
